package Stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/*
* stream流中间方法
*
* */
public class Demo2 {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("陌刀");
        list.add("横刀");
        list.add("双刀");
        list.add("伞");
        list.add("扇");
        Stream<String> stream = list.stream();

        //1、过滤方法
        System.out.println("=============过滤===========");
        stream.filter(s -> s.length()==2).forEach(System.out::println);
        System.out.println("===========================");

        //2、排序方法
        System.out.println("=============排序===========");
        System.out.println("=============升序===========");
        Stream<String> stream1 = list.stream();
        stream1.sorted().forEach(System.out::println);//默认升序
        System.out.println("=============降序===========");
        Stream<String> stream2 = list.stream();
        stream2.sorted((o1, o2) -> o2.compareTo(o1)).forEach(System.out::println);//逆序
        System.out.println("===========================");

        //3、拿到前几个
        System.out.println("===========取前几个=========");
        Stream<String> stream3 = list.stream();
        stream3.sorted((o1, o2) -> o2.compareTo(o1)).limit(2).forEach(System.out::println);
        System.out.println("===========================");

        //4、跳过前几个
        System.out.println("===========跳前几个=========");
        list.stream().sorted().skip(2).forEach(System.out::println);
        System.out.println("===========================");

        //5、去重==>自定义类去重需要重写hashcode和equals方法
        System.out.println("=============去重===========");
        list.stream().sorted((o1, o2) -> o2.compareTo(o1)).distinct().forEach(System.out::println);
        System.out.println("===========================");


        //6、加工元素
        System.out.println("=============加工===========");
        list.stream().map(s->"所用的武器是："+s).forEach(System.out::println);
        System.out.println("===========================");

        //7、合并流
        System.out.println("=============合并===========");
        Stream<String> concat = Stream.concat(list.stream().limit(3), list.stream().limit(1).map(String::toUpperCase));
        concat.forEach(System.out::println);
        System.out.println("===========================");
    }
}
